package com.example.guli.service.sms.service.Impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.guli.common.base.result.ResultCodeEnum;
import com.example.guli.service.sms.service.SmsService;
import com.example.guli.service.sms.util.SmsProperties;
import com.example.guli.service.base.exception.GuliException;
import com.google.gson.Gson;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    private SmsProperties smsProperties;
    //短信发送
    @SneakyThrows
    @Override
    public void send(String mobile, String checkCode) throws ClassCastException{

            //创建配置对象
            DefaultProfile profile = DefaultProfile.getProfile(
                    smsProperties.getRegionId(),
                    smsProperties.getKeyId(),
                    smsProperties.getKeySecret());
            //创建client对象
            IAcsClient client = new DefaultAcsClient(profile);
            //创建参数对象
            CommonRequest request = new CommonRequest();
            //组装参数对象
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");//发送请求的服务器
            request.setSysVersion("2017-05-25");//请求的版本号
            request.setSysAction("SendSms");//发送的请求是什么
            request.putQueryParameter("RegionId", smsProperties.getRegionId());
            request.putQueryParameter("PhoneNumbers", mobile);
            request.putQueryParameter("SignName", smsProperties.getSignName());
            request.putQueryParameter("TemplateCode", smsProperties.getTemplateCode());

            //转json，不转就需要自己拼
            Map<String, String> param = new HashMap<>();
            param.put("code",checkCode);
            Gson gson = new Gson();
            String json = gson.toJson(param);
//            request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");
            request.putQueryParameter("TemplateParam", json);

            //发送短信
            CommonResponse response = client.getCommonResponse(request);
            //System.out.println(response.getData());
            //得到就返回字符串格式的结果
            String data = response.getData();
            //解析响应结果
            HashMap<String,String> map = gson.fromJson(data, HashMap.class);
            String code = map.get("Code");
            String message = map.get("Message");

            //配置参考：短信服务->系统设置->国内消息设置
            //错误码参考：
            //https://help.aliyun.com/document_detail/101346.html?spm=a2c4g.11186623.6.613.3f6e2246sDg6Ry
            //控制所有短信流向限制（同一手机号：一分钟一条、一个小时五条、一天十条）
            if ("isv.BUSINESS_LIMIT_CONTROL".equals(code)) {
                log.error("短信发送过于频繁 " + "【code】" + code + ", 【message】" + message);
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL);
            }

            if(!"OK".equals(code)){
                  log.error("短信发送失败:"+code+" "+message);
                  throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR);
            }
    }
}
